﻿using System;
using KillBill.Business.Entities;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Suppliers.IView;
using KillBill.Services.DTO;
using KillBill.Presentation.Modules.Suppliers.SuppliersServiceReference;

namespace KillBill.Presentation.Modules.Suppliers.Presenter
{
    public class ProviderAddEditPresenter
    {
        public IProviderAddEdit View { get; set; }

        private readonly SuppliersServiceClient _service;
           
        public ProviderAddEditPresenter(IProviderAddEdit view)
        {
            _service = new SuppliersServiceClient();
            View = view;
            View.Provider = new Provider();
            View.IsEditing = false;
        }

        public ProviderAddEditPresenter(IProviderAddEdit view, Provider provider)
        {
            _service = new SuppliersServiceClient();
            View = view;
            View.Provider = provider;
            View.IsEditing = true;
        }

        public void Save()
        {
            KillBillDTO dto = View.IsEditing ?
                _service.UpdateProvider(View.Provider, Context.SessionID) :
                _service.AddProvider(View.Provider, Context.SessionID);
            View.ShowMessage((dto.Success) ?
                dto.Messages[0] : dto.Messages[0] + " " +
                dto.Messages[1]);
        }
    }
}
